<?php
/**
 * Created by PhpStorm.
 * User: jc234839
 * Date: 20/04/14
 * Time: 11:02 AM
 */
require 'init.php';

if (isset($_POST['submit'])) {
    // Server side form validation should Javascript be disabled

    // Clear existing messages in the message array
    unset($message);

    if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['confirmEmail']) || empty($_POST['password']) || empty($_POST['confirmPassword'])) {

        $message[] = 'All fields are required.';
    } else {
        if (strlen($_POST['password']) < 6) $message[] = 'Your password must be at least 6 characters long!';

        if ($_POST['confirmPassword'] !== $_POST['password']) $message[] = 'Passwords do not match!';

        if (filter_var(($_POST['email']), FILTER_VALIDATE_EMAIL) === false) {
            $message[] = 'Please enter a valid email address';
        } else if ($_POST['confirmEmail'] !== $_POST['email']) {
            $message[] = 'Email addresses do not match!';
        } else {
            $email = htmlentities($_POST['email']);
            $userExists = $user->userExists($email);

            if ($userExists) $message[] = 'An account with that email address has already been registered!';
        }
    }

    if (empty($message) === true) {
        $email = htmlentities($_POST['email']);
        $fullName = explode(" ", htmlentities($_POST['name']));
        $firstName = $fullName[0];
        $lastName = $fullName[count($fullName) - 1];
        $password = $_POST['password'];

        $success = $user->register($email, $firstName, $lastName, $password);

        if ($success) {
            header('Location: register.php?success');
            exit();
        } else {
            $message[] = 'Unable to register account';
        }
    }
}
if (isset($_GET['success']) && empty($_GET['success'])) {
    $message[] = 'Thank you for registering! An email has been sent to your nominated address';
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link href="css/main.css" rel="stylesheet" type="text/css">
    <script src="javascripts/validateForm.js"></script>
    <title>Register</title>
</head>
<body>
<div id="container">
    <div id="leftCogSplash"><img src="images/cogLeft.png" alt="Left Steampunk Cog"/></div>
    <div id="rightCogSplash"><img src="images/cogRight.png" alt="Right Steampunk Cog"/></div>
    <?php include "navbar.php"; ?>
    <div id="content">
        <h2>Register</h2>

        <form method="post" action="" name="registrationForm" id="registrationForm">
            <label for="name">Name:</label>
            <input type="text" name="name" id="name" size="35"
                   value="<?php if (isset($_POST['name'])) echo htmlentities($_POST['name']); ?>" onblur="validateName();"/>
            <br/>
            <label for="email">Email:</label>
            <input type="email" name="email" id="email" size="35"
                   value="<?php if (isset($_POST['email'])) echo htmlentities($_POST['email']); ?>"/>
            <br/>
            <label for="confirmEmail">Confirm Email:</label>
            <input type="email" name="confirmEmail" id="confirmEmail" size="35"
                   value="<?php if (isset($_POST['confirmEmail'])) echo htmlentities($_POST['confirmEmail']); ?>"
                   onblur="compareEmail();"/>
            <br/>
            <label for="password">Password:</label>
            <input type="password" name="password" id="password" size="20"/>
            <br/>
            <label for="confirmPassword">Confirm Password:</label>
            <input type="password" name="confirmPassword" id="confirmPassword" size="20" onblur="comparePassword();"/>
            <br/>

            <div id="submission">
                <input type="submit" name="submit" value="Register" id="registerButton"/>
            </div>
            <div id="statusBar">
                <?php
                if (empty($message) === false) {
                    echo "<script type='text/javascript'>document.getElementById('statusBar').style.display = 'block';</script>";
                    echo '<p>' . implode('<p></p>', $message) . '</p>';
                }
                ?>
            </div>
        </form>
    </div>
    <?php require 'footer.php' ?>
</div>
</body>
</html>
